import java.util.ArrayList;
import java.util.Random;

import org.nlogo.headless.HeadlessWorkspace;


public class RecuitSimule {
	private static double DELTA;
	private static double TO = 0.5;
	private static double alpha = 0.8;
	
	public static double updateTemperature(double TempAncien){
		return alpha * TempAncien;
	}
	
	
	public static void main(String[] args) {

		HeadlessWorkspace ws = HeadlessWorkspace.newInstance();
		try {
			ws.open("projet.nlogo");
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		int NB_NOEUDS = 500;
		int NB_ITER = 15;
		int[] matrice = HillClimbing.randomMat(60);
		System.out.println("matrice de depart random:" + HillClimbing.matString(matrice));
		int score = HillClimbing.mediane(ws, NB_NOEUDS, NB_ITER, matrice);
		System.out.println("son score de reference:" + score);
		int scoreprime;
		int oldScore;
		int cnt = 1;
		
		do {
			oldScore = score;
			ArrayList<int[]> voisins = new ArrayList<int[]>();
			int[] voisin1 = HillClimbing.varie(matrice, (int) DELTA, 0);
			int[] voisin2 = HillClimbing.varie(matrice, (int) DELTA, 1);
			int[] voisin3 = HillClimbing.varie(matrice, (int) DELTA, 2);
			voisins.add(voisin1);
			voisins.add(voisin2);
			voisins.add(voisin3);
			
			//Engendrer un voisin aléatoire S’ de S
			Random r = new Random();
			int[] voisine = voisins.get(r.nextInt(2));
			System.out.println("-voisin aleatoir num: --"+r.nextInt(2));
			scoreprime = HillClimbing.mediane(ws, NB_NOEUDS, NB_ITER, voisine);
			System.out.println("score de voisin aleatoire: " + scoreprime + " ;matrice voisin aleatoire: " + HillClimbing.matString(voisin1));
			//Calculer ∆ = f(S’) – f(S)
			DELTA = scoreprime - score;
			System.out.println("-delta-"+DELTA);
			double Temperature = DELTA/Math.log(TO);
			System.out.println("valeur de la temperature"+Temperature);
			int u;
			if (DELTA >0)
				score=scoreprime;
			else{
				u = r.nextInt(1);
				System.out.println("-u- = "+u);
				if(u < Math.exp(DELTA/Temperature)){
					 score = scoreprime;
				}
			}
				
			updateTemperature(Temperature);
			System.out.println("-nouvelle temperature = -"+Temperature);
				
			
		}
		while(score < oldScore);
		
		System.out.println("nombre de morts:" + score);
		
		try {
			ws.dispose();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		}
	}
